package com.nativetalk.service.feedback.search;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.feedback.TdNtFeedbackList;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.dao.TdNtFeedbackDao;
import com.nativetalk.service.feedback.insert.FeedbackInsertServiceImpl;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 16/5/19.
 */
@Service
public class FeedbackSearchServiceImpl implements FeedbackSearchService {
    private static Logger logger = Logger.getLogger(FeedbackInsertServiceImpl.class);
    @Resource
    private TdNtFeedbackDao tdNtFeedbackDao;

    @Override
    public RetInfo findFeedbackList(Page page) {
        String logInfo = this.getClass().getName() + ":findFeedbackList:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtFeedbackDao.findTdNtFeedbackListSize(page);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord/pageSize;
                } else {
                    totalPage = (totalRecord/pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                page.setCurrentPage((page.getCurrentPage()-1)*pageSize);

                List<TdNtFeedbackList> tdNtFeedbackLists = tdNtFeedbackDao.findTdNtFeedbackList(page);
                Map<String, Object> result = new HashMap<>();
                result.put("feedbacks", tdNtFeedbackLists);
                result.put("totalPage", totalPage);
                retInfo.setObj(result);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取消息成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("没有消息");
            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    @Override
    public RetInfo findFeedbackDetailList(Page page) {
        String logInfo = this.getClass().getName() + ":findFeedbackDetailList:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtFeedbackDao.findTdNtFeedbackListDEtailSize(page);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord/pageSize;
                } else {
                    totalPage = (totalRecord/pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                page.setCurrentPage((page.getCurrentPage()-1)*pageSize);

                List<TdNtFeedbackList> tdNtFeedbackLists = tdNtFeedbackDao.findTdNtFeedbackDetailList(page);
                Map<String, Object> result = new HashMap<>();
                result.put("feedbacks", tdNtFeedbackLists);
                result.put("totalPage", totalPage);
                retInfo.setObj(result);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取回复成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("没有回复");
            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
